from ytcommunity.shorturl.setting import *
from ytcommunity.shorturl.plugin import *

def request(request):
    context = {}
    context['host_url'] = request.host_url
    return context

def config(request):
    context = {}
    context['config'] = CONFIG
    return context

def plugin(request):
    context = {}
    
    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_SIDE]
    context['plugins'] = get_plugins_context(request, plugins)

    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_TOP]
    context['plugins_top'] = get_plugins_context(request, plugins)

    plugins = [plugin for plugin in PLUGINS if plugin['type'] == TYPE_BOTTOM]
    context['plugins_bottom'] = get_plugins_context(request, plugins)    
    
    return context

def get_plugins_context(request, plugins):
    context = []
    for plugin in plugins:
        if plugin.has_key('package'):
            plugin_module = __import__(plugin['package'], globals(), locals(), ['main'], -1)
            data = plugin_module.main(request)
        else:
            data = None
        plugin['view_path'] = plugin['view'] 
        context.append({'config':plugin, 'data':data})
    
    return context  

PROCESSORS = [request, config, plugin]
